package cn.Product.service.impl;

import cn.Product.service.ProductService;
import cn.Product.dao.ProductDao;
import cn.util.data.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductDao productDao;

    @Override
    public List<Map<String, Object>> getProductList(Map<String, Object> params) {
        return productDao.getProductList(params);
    }

    @Override
    public List<Map<String, Object>> getadminProductList(Map<String, Object> params) {
        params.put("end",20);
        if(params.containsKey("page")){
            params.put("start",Integer.parseInt(params.get("page").toString())-1*Integer.parseInt(params.get("end").toString()));
        }else {
            params.put("start",0);
        }
        if(params.get("SPLX")==null||params.get("SPLX").equals("")){
            params.put("SPLX",1);
        }
        return productDao.getadminProductList(params);
    }

    @Override
    public List<Map<String, Object>> getProductRandom(Map<String, Object> params) {
        return productDao.getProductRandom(params);
    }

    @Override
    public  Map<String, Object>  getProductType(Map<String, Object> params) {
        return productDao.getProductType(params);
    }
    @Override
    public List<Map<String, Object>> getTypeProductList(Map<String, Object> params) {
        params.put("end",20);
        if(params.containsKey("page")){
            params.put("start",Integer.parseInt(params.get("page").toString())-1*Integer.parseInt(params.get("end").toString()));
        }else {
            params.put("start",0);
        }
        return productDao.getTypeProductList(params);
    }

    @Override
    public Map<String, Object> getProductInfo(Map<String, Object> params) {
        Map<String,Object> ProductInfo=new HashMap<>();
        //单个商品信息
        Map<String,Object>INFO=productDao.getProductInfo(params);
        //商品搭配列表
        params.put("SPID",INFO.get("SPID"));
        List<Map<String,Object>> ProductArray=productDao.getProductArray(params);
        //获取每个搭配对应的商品
        for (Map<String,Object> map: ProductArray) {
            params.put("SPID",map.get("SPID"));
            //每个搭配 该有的商品
            map.put("ProductArrayInfo",productDao.getProductArrayInfo(params));
        }

        ProductInfo.put("ProductInfo",INFO);
        ProductInfo.put("ProductArray",ProductArray);
        return ProductInfo;
    }

    @Override
    public List<Map<String, Object>> getProductArray(Map<String, Object> params) {
        return productDao.getProductArray(params);
    }

    @Override
    public int addadminProduct(Map<String, Object> params) {
        return productDao.addadminProduct(params);
    }

    //修改商品
    public int updateadminProduct(Map<String, Object> params) {
        return productDao.updateadminProduct(params);
    }
    //删除商品
    public int daleteadminProduct(Map<String, Object> params) {
        return productDao.daleteadminProduct(params);
    }

    //修改类别
    public int updateadminProductType(Map<String, Object> params) {
        //修改级别
        int jibie= (int) params.get("jibie");
        String zidaun= (String) params.get("ziduan");
        if(zidaun.equals("LXMC")){
            params.put("ziduan","TYPE_NAME");
        }else if(zidaun.equals("SXID")){
            params.put("ziduan","ID_SX");
        }else if(zidaun.equals("LXURL")){
            params.put("ziduan","TYPE_URL");
        }
        if(jibie==1){
            params.put("sql","UPDATE YST_GOODS_YJTYPE SET YJ"+ params.get("ziduan")+"=#{value}  WHERE YJ_ID=#{lxid}");
        }else if(jibie==2){
            params.put("sql","UPDATE YST_GOODS_EJTYPE SET EJ"+ params.get("ziduan")+"=#{value}  WHERE EJ_ID=#{lxid}");
        }else if(jibie==3){
            params.put("sql","UPDATE YST_GOODS_SJTYPE SET SJ"+ params.get("ziduan")+"=#{value}  WHERE SJ_ID=#{lxid}");
        }
        return productDao.updateadminProductType(params);
    }

    //新增类别
    public int addadminProductType(Map<String, Object> params) {
        return productDao.addadminProductType(params);
    }

    @Override
    public int daleteadminProductType(Map<String, Object> params) {
        return productDao.daleteadminProductType(params);
    }
}
